<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm"
             :rules="dataRule"
             ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="130px"
             :inline="true"
    >
      <el-form-item label="职位名称" prop="jobName">
        <el-col :span="24">
          <el-input v-model="dataForm.jobName" placeholder="职位名称"></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="职位部门" prop="jobDeptement">
        <el-input v-model="dataForm.jobDeptement" placeholder="职位部门"></el-input>
      </el-form-item>
      <el-form-item label="联系人" prop="jobContact">
        <el-input v-model="dataForm.jobContact" placeholder="联系人"></el-input>
      </el-form-item>
      <el-form-item label="联系手机" prop="jobPhone">
        <el-input v-model="dataForm.jobPhone" placeholder="联系手机"></el-input>
      </el-form-item>
      <el-form-item label="联系固话" prop="jobFixPhone">
        <el-input v-model="dataForm.jobFixPhone" placeholder="联系固话"></el-input>
      </el-form-item>
      <el-form-item label="联系微信" prop="jobWechat">
        <el-input v-model="dataForm.jobWechat" placeholder="联系微信"></el-input>
      </el-form-item>
      <el-form-item label="联系邮箱" prop="jobEmail">
        <el-input v-model="dataForm.jobEmail" placeholder="联系邮箱"></el-input>
      </el-form-item>
      <el-form-item label="联系QQ" prop="jobQq">
        <el-input v-model="dataForm.jobQq" placeholder="联系qq"></el-input>
      </el-form-item>
      <el-form-item label="紧急招聘" prop="isUrgency">
        <el-col :span="22">
          <el-select v-model="dataForm.isUrgency" placeholder="是否急聘">
            <el-option label="急聘" value="1"></el-option>
            <el-option label="非急聘" value="0"></el-option>
          </el-select>
        </el-col>
      </el-form-item>
      <el-form-item label="所属行业" prop="industryOptions" label-width="115px">
        <el-col :span="22">
          <el-cascader
            v-model="industryOptionsArr"
            :options="industryOptions"
            @change="handleChangeIndustory">
          </el-cascader>
        </el-col>
      </el-form-item>
      <el-form-item label="工作地址" property="companyAddressOptions">
        <el-col :span="22">
          <el-cascader
            v-model="companyAddressOptionsArr"
            :options="companyAddressOptions"
            @change="handleChangeAddress">
          </el-cascader>
        </el-col>
      </el-form-item>
      <el-form-item label="详细地址" prop="jobDetailAddress" label-width="115px">
        <el-col :span="24">
          <el-input v-model="dataForm.jobDetailAddress" placeholder="详细地址"></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="请选择企业" prop="companyId">
        <el-col :span="22">
          <el-select
            v-model="dataForm.companyId"
            clearable
            placeholder="请选择"
            @change="companySelect"
          >
            <el-option
              v-for="item in selectCompany"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-col>
      </el-form-item>
      <el-form-item label="薪资范围" required>
        <el-col :span="11">
          <el-form-item prop="jobSalaryStart">
            <el-input v-model="dataForm.jobSalaryStart" style="width: 100%;" placeholder="薪资范围"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="1">-</el-col>
        <el-col :span="11">
          <el-form-item prop="jobSalaryEnd">
            <el-input v-model="dataForm.jobSalaryEnd" style="width: 100%;" placeholder="薪资范围"></el-input>
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item label="年龄要求" required>
        <el-col :span="11">
          <el-form-item prop="jobAgeStart">
            <el-input v-model="dataForm.jobAgeStart" style="width: 100%;" placeholder="年龄要求"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="1">-</el-col>
        <el-col :span="11">
          <el-form-item prop="jobAgeEnd">
            <el-input v-model="dataForm.jobAgeEnd" style="width: 100%;" placeholder="年龄要求"></el-input>
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item label="证书要求" prop="detailPosition" >
          <el-select
            v-model="positionCertifiCate"
            clearable
            multiple
            @change="positionCertifiCateOptionChange"
            placeholder="请选择">
            <el-option
              v-for="item in certifiCateOption"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      <el-form-item label="职位福利" prop="detailPosition" label-width="100px">
            <el-select
              v-model="positionWalfare"
              clearable
              multiple
              @change="positionOptionChange"
              placeholder="请选择">
              <el-option
                v-for="item in walfareOption"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
        </el-form-item>
      <el-form-item label="职位描述" prop="jobDescript">
        <el-input
          style="width: 500px"
          type="textarea"
          v-model="dataForm.jobDescript"
          placeholder="职位描述"
          :rows = "5"
          :cols="50"
          maxlength="100"
          show-word-limit>
        </el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          companyAddressOptionsArr:[],
          jobName: '',
          jobDescript: '',
          jobDeptement: '',
          jobContact: '',
          jobPhone: '',
          jobFixPhone: '',
          jobWechat: '',
          jobEmail: '',
          jobQq: '',
          isUrgency: '',                     //是否是急聘
          companyId: '',
          tradeId: '',
          classifyId: '',
          jobTypeId: '',
          provincesId: '',
          citysId: '',
          areasId: '',
          jobSalaryStart: '',
          jobSalaryEnd: '',
          jobApproveStatus: '',
          jobIssueStatus: '',
          jobRecruitStatus: '',
          jobIsPass: '',
          jobAgeStart: '',
          jobAgeEnd: '',
          jobDetailAddress: '',
        },
        dataRule: {
          jobName: [
            { required: true, message: '职位名称不能为空', trigger: 'blur' }
          ],
          jobDescript: [
            { required: true, message: '职位描述不能为空', trigger: 'blur' }
          ],
          jobDeptement: [
            { required: true, message: '职位部门不能为空', trigger: 'blur' }
          ],
          jobContact: [
            { required: true, message: '联系人不能为空', trigger: 'blur' }
          ],
          jobPhone: [
            { required: true, message: '联系手机不能为空', trigger: 'blur' }
          ],
          jobFixPhone: [
            { required: true, message: '联系固话不能为空', trigger: 'blur' }
          ],
          jobWechat: [
            { required: true, message: '联系微信不能为空', trigger: 'blur' }
          ],
          jobEmail: [
            { required: true, message: '联系邮箱不能为空', trigger: 'blur' }
          ],
          jobQq: [
            { required: true, message: '联系qq不能为空', trigger: 'blur' }
          ],
          isUrgency: [
            { required: true, message: '是否紧急招聘不能为空', trigger: 'blur' }
          ],
          companyId: [
            { required: true, message: '企业ID不能为空', trigger: 'blur' }
          ],
          jobSalaryStart: [
            { required: true, message: '请输入起始薪资', trigger: 'blur' }
          ],
          jobSalaryEnd: [
            { required: true, message: '请输入结束薪资', trigger: 'blur' }
          ],
          jobAgeStart: [
            { required: true, message: '请输入起始年龄', trigger: 'blur' }
          ],
          jobAgeEnd: [
            { required: true, message: '请输入结束年龄', trigger: 'blur' }
          ],
          jobDetailAddress: [
            { required: true, message: '详细地址不能为空', trigger: 'blur' }
          ]
        },
        userId:'',                        //当前的userId
        certifiCateOption:[],             //证书列表
        walfareOption:[],                 //福利列表


        positionCertifiCate:[],
        positionWalfare:[],

        industryOptions:[],               //行业的选项卡
        industryOptionsArr:[],            //选中的值

        companyAddressOptions:[],         //地址的选项卡
        companyAddressOptionsArr:[],      //选中的值

        selectCompany:[]                  //选择企业的Select

      }
    },
    created () {
      this.getUserInfo()
      this.loadingIndustory()
      this.loadingCompanyAddress()
      this.loadingCertifiCate()
      this.loadingWalfare()
    },
    beforeMount () {

    },
    mounted () {

    },
    methods: {

      /*加载已经登录的用户*/
      getUserInfo () {
        this.$http({
          url: this.$http.adornUrl('/sys/user/info'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.loading = false
            this.userId = data.user.userId
          }
        })
      },
      //加载行业选项
      loadingIndustory(){
        this.$http({
          //url: this.$http.adornUrl('/workjobs/workjob/tree-option-ccj'),
          url: this.$http.adornUrl('/common/tradecategory/trade-category'),
          method: 'get',
        }).then(({data}) => {
          if (data && data.code === 0){
            this.industryOptions = data.data
          }
        })
      },
      //加载企业地址选项
      loadingCompanyAddress(){
        this.$http({
          url: this.$http.adornUrl('/companys/company/tree-option-pcr'),
          method: 'get',
        }).then(({data}) => {
          if (data && data.code === 0){
            this.companyAddressOptions = data.data
          }
        })
      },
      //加载证书列表
      loadingCertifiCate(){
        this.$http({
          url: this.$http.adornUrl('/common/certificate/select-option'),
          method: 'get',
        }).then(({data}) => {
          if (data && data.code === 0){
            this.certifiCateOption = data.data
          }
        })
      },
      //加载福利列表
      loadingWalfare(){
        this.$http({
          url: this.$http.adornUrl('/common/welfare/select-option'),
          method: 'get',
        }).then(({data}) => {
          if (data && data.code === 0){
            this.walfareOption = data.data
          }
        })
      },
      init (id) {
        console.log("id is Value ===>",id)
        this.dataForm.id = id || 0


        if (id == undefined){
          const userId = JSON.parse(sessionStorage.getItem('user_server')).userId
          this.$http({
            url:this.$http.adornUrl('/workjobs/workjob/company-option/' + userId),
            method:'get'
          }).then(data =>{
            console.log("data ===>>",data)
            if (data.data.code === 0){
              if (data.data.data.length === 0){
                this.$message({
                  type:'warning',
                  message:'请先创建企业信息'
                })
                this.visible = false
                return
              }else{
                this.visible = true
                this.selectCompany = data.data.data
              }
            }
          }).catch(err =>{
            Promise.reject(err)
          })
        }else{
          this.$http({
            url:this.$http.adornUrl('/workjobs/workjob/address-option/' + id),
            method:'get'
          })
            .then(data => {
              if (data && data.data.code === 0){
                this.companyAddressOptionsArr = data.data.data
              }
            })

          this.$http({
            url:this.$http.adornUrl('/workjobs/workjob/industory-option/' + id),
            method:'get'
          })
            .then(data => {
              if (data && data.data.code === 0){
                this.industryOptionsArr = data.data.data
              }
            })

          this.$http({
            url:this.$http.adornUrl('/workjobs/workjob/certificates-option/' + id),
            method:'get'
          })
            .then(data => {
              if (data && data.data.code === 0){
                this.positionCertifiCate = data.data.data
              }
            })

          this.$http({
            url:this.$http.adornUrl('/workjobs/workjob/walfare-option/' + id),
            method:'get'
          })
            .then(data => {
              if (data && data.data.code === 0){
                this.positionWalfare = data.data.data
              }
            })

          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            if (this.dataForm.id) {
              this.$http({
                url: this.$http.adornUrl(`/workjobs/workjob/info/${this.dataForm.id}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm.jobName = data.workJob.jobName
                  this.dataForm.jobDescript = data.workJob.jobDescript
                  this.dataForm.jobDeptement = data.workJob.jobDeptement
                  this.dataForm.jobContact = data.workJob.jobContact
                  this.dataForm.jobPhone = data.workJob.jobPhone
                  this.dataForm.jobFixPhone = data.workJob.jobFixPhone
                  this.dataForm.jobWechat = data.workJob.jobWechat
                  this.dataForm.jobEmail = data.workJob.jobEmail
                  this.dataForm.jobQq = data.workJob.jobQq
                  this.dataForm.isUrgency = data.workJob.isUrgency.toString()
                  this.dataForm.companyId = data.workJob.companyId
                  this.dataForm.tradeId = data.workJob.tradeId
                  this.dataForm.classifyId = data.workJob.classifyId
                  this.dataForm.jobTypeId = data.workJob.jobTypeId
                  this.dataForm.provincesId = data.workJob.provincesId
                  this.dataForm.citysId = data.workJob.citysId
                  this.dataForm.areasId = data.workJob.areasId
                  this.dataForm.jobSalaryStart = data.workJob.jobSalaryStart
                  this.dataForm.jobSalaryEnd = data.workJob.jobSalaryEnd
                  this.dataForm.jobApproveStatus = data.workJob.jobApproveStatus
                  this.dataForm.jobIssueStatus = data.workJob.jobIssueStatus
                  this.dataForm.jobRecruitStatus = data.workJob.jobRecruitStatus
                  this.dataForm.jobIsPass = data.workJob.jobIsPass
                  this.dataForm.jobAgeStart = data.workJob.jobAgeStart
                  this.dataForm.jobAgeEnd = data.workJob.jobAgeEnd
                  this.dataForm.jobDetailAddress = data.workJob.jobDetailAddress
                }
              })
            }
          })
        }
      },
      // 表单提交
      dataFormSubmit () {

        /*表单信息校验*/
        if (this.dataForm.jobName == ''){
          this.$message({
            type:'warning',
            message:'请填写职位名称'
          })
          return
        }

        if (this.dataForm.jobDeptement == ''){
          this.$message({
            type:'warning',
            message:'请填写部门信息'
          })
          return
        }

        if (this.dataForm.jobContact == ''){
          this.$message({
            type:'warning',
            message:'请填写联系人'
          })
          return
        }

        if (this.dataForm.jobPhone == ''){
          this.$message({
            type:'warning',
            message:'请填写联系电话'
          })
          return
        }

        if (this.dataForm.jobFixPhone == ''){
          this.$message({
            type:'warning',
            message:'请填写联系固话'
          })
          return
        }

        if (this.dataForm.jobWechat == ''){
          this.$message({
            type:'warning',
            message:'请填写联系微信'
          })
          return
        }

        if (this.dataForm.jobQq == ''){
          this.$message({
            type:'warning',
            message:'请填写联系QQ'
          })
          return
        }

        if (this.dataForm.jobEmail == ''){
          this.$message({
            type:'warning',
            message:'请填写联系邮箱'
          })
          return
        }

        if (this.dataForm.jobName == ''){
          this.$message({
            type:'warning',
            message:'请填写职位名称'
          })
          return
        }

        if (this.dataForm.isUrgency == ''){
          this.$message({
            type:'warning',
            message:'请选择是否紧急招聘'
          })
          return
        }

        if (this.industryOptionsArr.length == 0){
          this.$message({
            type:'warning',
            message:'请选择职位行业'
          })
          return
        }

        if (this.companyAddressOptionsArr.length == 0){
          this.$message({
            type:'warning',
            message:'请选择地址信息'
          })
          return
        }

        if (this.dataForm.jobSalaryStart == ''){
          this.$message({
            type:'warning',
            message:'请选择起始薪资'
          })
          return
        }

        if (this.dataForm.jobSalaryEnd == ''){
          this.$message({
            type:'warning',
            message:'请选择结束的薪资'
          })
          return
        }

        if ((parseFloat(this.dataForm.jobSalaryStart))<0 || (parseFloat(this.dataForm.jobSalaryEnd))<0){
          this.$message({
            type:'warning',
            message:'薪资输入不合法'
          })
          return
        }


        if (isNaN(parseFloat(this.dataForm.jobSalaryStart))||isNaN(parseFloat(this.dataForm.jobSalaryEnd))){
          this.$message({
            type:'warning',
            message:'不可以输入非数字的薪资'
          })
          return
        }

        if (parseFloat(this.dataForm.jobSalaryStart) > parseFloat(this.dataForm.jobSalaryEnd)){
          this.$message({
            type:'warning',
            message:'起始薪资位不可以大于结束的薪资位'
          })
          return
        }

        if (this.dataForm.jobAgeStart == ''){
          this.$message({
            type:'warning',
            message:'请填写起始年龄段范围'
          })
          return
        }

        if (this.dataForm.jobSalaryEnd == ''){
          this.$message({
            type:'warning',
            message:'请填写结束年龄段范围'
          })
          return
        }

        if (parseInt(this.dataForm.jobAgeStart) < 18 || parseInt(this.dataForm.jobAgeStart) >65){
          this.$message({
            type:'warning',
            message:'起始年龄范围 18 ~ 65'
          })
          return
        }

        if (parseInt(this.dataForm.jobAgeEnd) < 18 || parseInt(this.dataForm.jobAgeEnd) >65){
          this.$message({
            type:'warning',
            message:'结束年龄范围 18 ~ 65'
          })
          return
        }

        if (isNaN(parseInt(this.dataForm.jobAgeStart)) || isNaN(parseInt(this.dataForm.jobAgeEnd))){
          this.$message({
            type:'warning',
            message:'非法的年龄数字'
          })
          return
        }

        if (parseInt(this.dataForm.jobAgeStart) > parseInt(this.dataForm.jobAgeEnd)){
          this.$message({
            type:'warning',
            message:'起始年龄端不能大于结束年龄段'
          })
          return
        }

        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/workjobs/workjob/${!this.dataForm.id ? 'serverSave' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'jobName': this.dataForm.jobName,
                'jobDescript': this.dataForm.jobDescript,
                'jobDeptement': this.dataForm.jobDeptement,
                'jobContact': this.dataForm.jobContact,
                'jobPhone': this.dataForm.jobPhone,
                'jobFixPhone': this.dataForm.jobFixPhone,
                'jobWechat': this.dataForm.jobWechat,
                'jobEmail': this.dataForm.jobEmail,
                'jobQq': this.dataForm.jobQq,
                'isUrgency': this.dataForm.isUrgency,
                'companyId': this.dataForm.companyId,
                'tradeId': this.dataForm.tradeId,
                'classifyId': this.dataForm.classifyId,
                'jobTypeId': this.dataForm.jobTypeId,
                'provincesId': this.dataForm.provincesId,
                'citysId': this.dataForm.citysId,
                'areasId': this.dataForm.areasId,
                'jobSalaryStart': this.dataForm.jobSalaryStart,
                'jobSalaryEnd': this.dataForm.jobSalaryEnd,
                'jobApproveStatus': this.dataForm.jobApproveStatus,
                'jobIssueStatus': this.dataForm.jobIssueStatus,
                'jobRecruitStatus': this.dataForm.jobRecruitStatus,
                'jobIsPass': this.dataForm.jobIsPass,
                'jobAgeStart': this.dataForm.jobAgeStart,
                'jobAgeEnd': this.dataForm.jobAgeEnd,
                'jobDetailAddress': this.dataForm.jobDetailAddress,
                'positionCertifiCate':this.positionCertifiCate,
                'positionWalfare':this.positionWalfare,
                'createUserId':this.userId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
                //清空表单
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      //职位福利change事件
      positionOptionChange(value){
        console.log("positionOpti ====>",value)
      },
      //职位证书要求 change事件
      positionCertifiCateOptionChange(value){
        console.log("position certifiCate ==>>",value)
      },
      //选择企业的select框
      companySelect(value){

      },
      handleChangeIndustory(value){
        console.log("行业的value ===》",value)
        this.dataForm.tradeId = value[0]
        this.dataForm.classifyId = value[1]
        this.dataForm.jobTypeId = value[2]
        console.log("行业1 ==>" ,this.dataForm.tradeId)
        console.log("行业2 ==>" ,this.dataForm.classifyId)
        console.log("行业3 ==>" ,this.dataForm.jobTypeId)
      },
      handleChangeAddress(value){
        console.log("地址的value ===》",value)
        this.dataForm.provincesId = value[0]
        this.dataForm.citysId = value[1]
        this.dataForm.areasId = value[2]
        console.log("地址1 ==》",this.dataForm.provincesId)
        console.log("地址2 ==》",this.dataForm.citysId)
        console.log("地址3 ==》",this.dataForm.areasId)
      }
    }
  }
</script>
